<template>
    <div class="playlist-container">
    <div class="top-wrap">
      <div class="img-wrap">
        <!-- 封面 -->
        <img :src="playlist.coverImgUrl" alt="" />
      </div>
      <div class="info-wrap">
        <!-- 名字 -->
        <p class="title">{{ playlist.name }}</p>
        <div class="author-wrap">
          <!-- 头像 -->
          <!-- 神经病VUE,不加v-if="playlist.creator !== undefined" 报错 -->
          <img class="avatar" :src="playlist.creator.avatarUrl" v-if="playlist.creator !== undefined" alt="" />
          <!-- 名字 -->
          <span class="name" v-if="playlist.creator !== undefined">{{ playlist.creator.nickname }}</span>
          <!-- 时间 -->
          <span class="time">{{ playlist.createTime }}</span>
        </div>
        <div class="play-wrap">
          <span class="el-icon-video-play icon-circle-play"></span>
          <span class="text">播放全部</span>
        </div>
        <div class="tag-wrap">
          <span class="title">标签:</span>
          <ul>
            <li v-for="(item, index) in playlist.tags" :key="index">{{ item }}</li>
          </ul>
        </div>
        <div class="desc-wrap">
          <span class="title">简介:</span>
          <span class="desc">{{ playlist.description }}</span>
        </div>
      </div>
    </div>
    <el-tabs v-model="activeIndex">
      <el-tab-pane label="歌曲列表" name="1">
        <table class="el-table playlit-table">
          <thead>
            <th></th>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr class="el-table__row" v-for="(item, index) in playlist.tracks" :key="index">
              <td>{{ index + 1 }}</td>
              <td>
                <div class="img-wrap" @click="play(item.id)">
                  <img :src="item.al.picUrl+'?param=130y130'" alt="" />
                  <span class="el-icon-video-play icon-play"></span>
                </div>
              </td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <!-- 名称 -->
                    <span>{{ item.name }}</span>
                    <!-- mv图标 -->
                    <span v-if="item.mv !== 0" class="el-icon-video-play icon-mv" @click="toMv(item.mv)"></span>
                  </div>
                </div>
              </td>
              <td>{{ item.ar[0].name }}</td>
              <td>{{ item.al.name }}</td>
              <td>{{ item.dt  }}</td>
            </tr>
          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane :label="`评论(${total})`" name="2">
        <!-- 精彩评论 -->
        <div class="comment-wrap" v-if="hotComments !== undefined && hotComments.length !== 0">
          <p class="title">
            精彩评论<span class="number">({{ hotComments.length }})</span>
          </p>
          <div class="comments-wrap">
            <!-- 评论 -->
            <div class="item" v-for="(item, index) in hotComments" :key="index">
              <div class="icon-wrap">
                <!-- 头像 -->
                <img :src="item.user.avatarUrl+'?param=50y50'" alt="" />
              </div>
              <div class="content-wrap">
                <div class="content">
                  <span class="name">{{ item.user.nickname }}</span>
                  <span class="comment">：{{ item.content }}</span>
                </div>
                <!-- 回复 -->
                <div class="re-content" v-if="item.beReplied.length !== 0">
                  <span class="name">{{ item.beReplied[0].user.nickname }}</span>
                  <span class="comment">：{{ item.beReplied[0].content }}</span>
                </div>
                <div class="date">{{ item.time }}</div>
              </div>
            </div>
          </div>
        </div>
        <!-- 最新评论 -->
        <div class="comment-wrap">
          <p class="title">
            最新评论<span class="number">({{ total }})</span>
          </p>
          <div class="comments-wrap">
            <!-- 评论 -->
            <div class="item" v-for="(item, index) in comments" :key="index">
              <div class="icon-wrap">
                <!-- 头像 -->
                <img :src="item.user.avatarUrl+'?param=50y50'" alt="" />
              </div>
              <div class="content-wrap">
                <div class="content">
                  <span class="name">{{ item.user.nickname }}</span>
                  <span class="comment">：{{ item.content }}</span>
                </div>
                <!-- 回复 -->
                <div class="re-content" v-if="item.beReplied.length !== 0">
                  <span class="name">{{ item.beReplied[0].user.nickname }}</span>
                  <span class="comment">：{{ item.beReplied[0].content }}</span>
                </div>
                <div class="date">{{ item.time }}</div>
              </div>
            </div>
          </div>
        </div>
        <!-- 分页器 -->
        <el-pagination @current-change="handleCurrentChange" background layout="prev, pager, next" :total="total" :current-page="page" :page-size="pageSize">
        </el-pagination>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { playMusic } from  '../api/index'
import { detail, hotComment, playlistComment } from '../api/playlist'
export default {
    data(){
        return{
        id: '',
        activeIndex: '1',
        total: 0, // 总条数
        page: 1, // 页码
        pageSize: 10,
        playlist: {},//歌单详情数据
        hotComments: [],//热门品论
        comments: [],//最新评论
        hotCount:0//热门品论个数
        }
    },
    created(){
     this.getList()
     this.getComment()
     this.newComment()
    },
    methods:{
       play(id){
            playMusic({
                id
            }).then(res => {
                 let url = res.data[0].url
            // 设置给父组件的播放地址
                 this.$parent.musicUrl = url
            })
        },
      //获取歌单详情
        getList(){
            detail({
              id:this.$route.query.q,
                type:2
            }).then(res => {
              console.log(res)
               this.playlist = res.playlist
               for(let i = 0;i<this.playlist.tracks.length;i++){
                // 获取总毫秒数
                let duration = this.playlist.tracks[i].dt
                // 假设  455600 毫秒
                // 秒  455600/1000
                // 分 455 / 60
                // 秒 455 % 60
                let min = parseInt(duration / 1000 / 60)
                if(min < 10) {
                    min = '0' + min
                }
                let sec = parseInt((duration / 1000) % 60)
                if(sec < 10) {
                    sec = '0' + sec
                }
                this.playlist.tracks[i].dt = `${min}:${sec}`
            }
            })
        },
        //获取评论
        getComment(){
             hotComment({
               id:this.$route.query.q,
              type:2
             }).then(res => {
               console.log(res)
               this.hotComments = res.hotComments
               //保存个数
               this.total = res.total
             })
        },
        newComment(){
          playlistComment({
            id:this.$route.query.q,
                limit:10,
                offset:(this.page-1)*10
          }).then(res => {
             //总个数
              this.total = res.total
              //品论数据
              this.comments = res.comments
          })
        },
        handleCurrentChange(val){
            console.log(val)
            this.page = val
            this.newComment()
        }
    }
}
</script>


<style lang="scss" scoped>
.playlist-container {
    margin: auto;
    padding-top: 40px;
    .top-wrap {
        display: flex;
        span {
            font-size: 17px;
        }
        .img-wrap {
            margin-right: 30px;
            img {
                width: 230px;
                height: 230px;
            }
        }
        .info-wrap {
            .title {
                margin-bottom: 20px;
            }
            .author-wrap {
                display: flex;
                align-items: center;
                margin-bottom: 25px;
                img {
                    width: 35px;
                    height: 35px;
                    border-radius: 50%;
                    margin-right: 10px;
                }
                .name {
                    margin-right: 10px;
                }
                .time {
                    font-size: 14px;
                }
            }
            .play-wrap {
                width: 120px;
                height: 35px;
                border-radius: 4px;
                background: linear-gradient(to right, #e85e4d, #c6483c);
                display: flex;
                align-items: center;
                justify-content: center;
                margin-bottom: 25px;
                span {
                    color: white;
                    &.el-icon-video-play {
                        margin-right: 8px;
                    }
                    &.text {
                        font-size: 16px;
                    }
                }
            }
            .tag-wrap {
                display: flex;
                margin-bottom: 15px;
                span {
                    margin: 0;
                }
                ul {
                    display: flex;
                    align-items: center;
                }
                li {
                    font-size: 15px;
                    &:not(:last-child)::after {
                        content: '/';
                        margin: 0 4px;
                    }
                }
            }
            .desc-wrap {
                span {
                    &:last-child {
                        font-size: 15px;
                    }
                }
            }
            span {
                &:first-child {
                    margin-right: 10px;
                }
            }
        }
    }
    .comment-wrap {
        margin-bottom: 70px;
        .title {
            font-size: 20px;
            .number {
                color: black;
                font-size: 18px;
            }
        }
        .item {
            display: flex;
            padding-top: 20px;
            .icon-wrap {
                margin-right: 15px;
                img {
                    width: 50px;
                    height: 50px;
                    border-radius: 50%;
                }
            }
            &:not(:last-child) {
                .content-wrap {
                    border-bottom: 1px solid #f2f2f1;
                }
            }
            .date {
                color: #bebebe;
                font-size: 14px;
            }
            .content-wrap {
                padding-bottom: 20px;
                flex: 1;
                .name {
                    color: #517eaf;
                    font-size: 14px;
                }
                .comment {
                    font-size: 14px;
                }
                .re-content {
                    padding: 10px;
                    background-color: #e6e5e6;
                    border-radius: 5px;
                }
            }
        }
    }
}
.playlist-container .comment-wrap .item .content-wrap .content,
.playlist-container .comment-wrap .item .content-wrap .re-content {
    margin-bottom: 10px;
}

</style>